package edu.pku.group6.dao;

import edu.pku.group6.entities.SupplyDemand;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class SupplyDemand.
 * 
 * @see edu.pku.group6.dao.SupplyDemand
 * @author MyEclipse Persistence Tools
 */

public class SupplyDemandDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(SupplyDemandDAO.class);

	// property constants
	public static final String ENTERPRISENAME = "enterprisename";

	public static final String TYPE = "type";

	public static final String INDUSTRY = "industry";

	public static final String REGION = "region";

	public static final String CONTACT = "contact";

	public static final String TEL = "tel";

	public static final String VISIT = "visit";

	public static final String DETAIL = "detail";

	public void save(SupplyDemand transientInstance) {
		log.debug("saving SupplyDemand instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SupplyDemand persistentInstance) {
		log.debug("deleting SupplyDemand instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SupplyDemand findById(java.lang.Integer id) {
		log.debug("getting SupplyDemand instance with id: " + id);
		try {
			SupplyDemand instance = (SupplyDemand) getSession().get(
					"edu.pku.group6.dao.SupplyDemand", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SupplyDemand instance) {
		log.debug("finding SupplyDemand instance by example");
		try {
			List results = getSession().createCriteria(
					"edu.pku.group6.dao.SupplyDemand").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SupplyDemand instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SupplyDemand as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEnterprisename(Object enterprisename) {
		return findByProperty(ENTERPRISENAME, enterprisename);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByIndustry(Object industry) {
		return findByProperty(INDUSTRY, industry);
	}

	public List findByRegion(Object region) {
		return findByProperty(REGION, region);
	}

	public List findByContact(Object contact) {
		return findByProperty(CONTACT, contact);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByVisit(Object visit) {
		return findByProperty(VISIT, visit);
	}

	public List findByDetail(Object detail) {
		return findByProperty(DETAIL, detail);
	}

	public List findAll() {
		log.debug("finding all SupplyDemand instances");
		try {
			String queryString = "from SupplyDemand";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SupplyDemand merge(SupplyDemand detachedInstance) {
		log.debug("merging SupplyDemand instance");
		try {
			SupplyDemand result = (SupplyDemand) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SupplyDemand instance) {
		log.debug("attaching dirty SupplyDemand instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SupplyDemand instance) {
		log.debug("attaching clean SupplyDemand instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}